package conf

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func LoadProp(path string) (map[string]string, error) {
	// 打开properties文件
	file, err := os.Open(path)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	defer func(file *os.File) {
		err = file.Close()
		if err != nil {
			return
		}
	}(file)

	// 读取properties文件
	scanner := bufio.NewScanner(file)
	properties := make(map[string]string)
	for scanner.Scan() {
		line := scanner.Text()
		if strings.HasPrefix(line, "#") || strings.HasPrefix(line, "!") {
			// 忽略注释行
			continue
		}
		parts := strings.SplitN(line, "=", 2)
		if len(parts) != 2 {
			// 忽略格式不正确的行
			continue
		}
		key := strings.TrimSpace(parts[0])
		value := strings.TrimSpace(parts[1])
		properties[key] = value
	}
	if err = scanner.Err(); err != nil {
		fmt.Println(err)
		return nil, err
	}
	return properties, nil

}
